package cn.sh.ideal.web.configuration.management.controller;

import cn.sh.ideal.web.configuration.management.entity.MiroServiceEntity;
import cn.sh.ideal.web.view.display.entity.ProcessInforEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName
 * @Description Too
 * @Author ME
 * @Data 2018/07/18 18:51
 **/
@Controller
@RequestMapping(value = "/data")
public class MiroServiceManagementController {

    @Value("${server_address}")
    private String serverAddress;

    private RestTemplate rs = new RestTemplate();

    @RequestMapping(value = "selectMiroService")
    @ResponseBody
    public String getMiroServiceList(@RequestParam("miroservice")String miroservice,
                                     @RequestParam("page")String page,
                                     @RequestParam("size")String size){
        String url = serverAddress+"/selectMiroService?miroservice="+miroservice
                      +"&page="+(StringUtils.isBlank(page)?"":page)
                      +"&size="+(StringUtils.isBlank(size)?"":size) ;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
        ResponseEntity<String> exchange = rs.exchange(url,HttpMethod.GET, entity, String.class);
        String result = exchange.getBody();
        return result;
    }

    @RequestMapping(value = "insertMiroService",method=RequestMethod.POST)
    @ResponseBody
    public int insertMiroServiceinfo(@RequestBody List<MiroServiceEntity> miroServiceEntity,
                                     HttpServletRequest request,
                                     HttpServletResponse response){
        int dao=0;
        ArrayList<MiroServiceEntity> miroServiceEntityList = (ArrayList<MiroServiceEntity>) JSON.parseObject(String.valueOf(miroServiceEntity), new TypeReference<ArrayList<MiroServiceEntity>>(){});

        for(int i=0;i<miroServiceEntityList.size();i++){
            String url = serverAddress+"/insertMiroService?id="+miroServiceEntityList.get(i).getId()
                    +"&miroservice="+(StringUtils.isBlank(miroServiceEntityList.get(i).getMiroservice())?"":miroServiceEntityList.get(i).getMiroservice())
                    +"&namespace="+(StringUtils.isBlank(miroServiceEntityList.get(i).getNamespace())?"":miroServiceEntityList.get(i).getNamespace())
                    +"&containerName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getContainerName())?"":miroServiceEntityList.get(i).getContainerName())
                    +"&service="+(StringUtils.isBlank(miroServiceEntityList.get(i).getService())?"":miroServiceEntityList.get(i).getService())
                    +"&hostid="+(StringUtils.isBlank(miroServiceEntityList.get(i).getHostid())?"":miroServiceEntityList.get(i).getHostid())
                    +"&port="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPort())?"":miroServiceEntityList.get(i).getPort())
                    +"&podName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPodName())?"":miroServiceEntityList.get(i).getPodName());
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
            String result = responseEntity.getBody();
            if("1".equals(result)&&StringUtils.isNotBlank(result)){
                dao++;
            }
        }
        return dao;

    }

    @RequestMapping(value = "updateMiroService")
    @ResponseBody
    public int updateMiroServiceinfo(@RequestBody List<MiroServiceEntity> miroServiceEntity,
                                     HttpServletRequest request,
                                     HttpServletResponse response){
        int dao=0;
        ArrayList<MiroServiceEntity> miroServiceEntityList = (ArrayList<MiroServiceEntity>) JSON.parseObject(String.valueOf(miroServiceEntity), new TypeReference<ArrayList<MiroServiceEntity>>(){});
        for(int i=0;i<miroServiceEntityList.size();i++){
            String url = serverAddress+"/updateMiroService?id="+miroServiceEntityList.get(i).getId()
                    +"&miroservice="+(StringUtils.isBlank(miroServiceEntityList.get(i).getMiroservice())?"":miroServiceEntityList.get(i).getMiroservice())
                    +"&namespace="+(StringUtils.isBlank(miroServiceEntityList.get(i).getNamespace())?"":miroServiceEntityList.get(i).getNamespace())
                    +"&containerName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getContainerName())?"":miroServiceEntityList.get(i).getContainerName())
                    +"&service="+(StringUtils.isBlank(miroServiceEntityList.get(i).getService())?"":miroServiceEntityList.get(i).getService())
                    +"&hostid="+(StringUtils.isBlank(miroServiceEntityList.get(i).getHostid())?"":miroServiceEntityList.get(i).getHostid())
                    +"&port="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPort())?"":miroServiceEntityList.get(i).getPort())
                    +"&podName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPodName())?"":miroServiceEntityList.get(i).getPodName());
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
            String result = responseEntity.getBody();
            if("1".equals(result)&&StringUtils.isNotBlank(result)){
                dao++;
            }
        }

        return dao;

    }


    @RequestMapping(value = "deleteMiroService")
    @ResponseBody
    public int deleteMiroServiceinfo(@RequestBody List<MiroServiceEntity> miroServiceEntity,
                                     HttpServletRequest request,
                                     HttpServletResponse response){
        int dao=0;
        ArrayList<MiroServiceEntity> miroServiceEntityList = (ArrayList<MiroServiceEntity>) JSON.parseObject(String.valueOf(miroServiceEntity), new TypeReference<ArrayList<MiroServiceEntity>>(){});

        for(int i=0;i<miroServiceEntityList.size();i++){
            String url = serverAddress+"/deleteMiroService?id="+miroServiceEntityList.get(i).getId()
                    +"&miroservice="+(StringUtils.isBlank(miroServiceEntityList.get(i).getMiroservice())?"":miroServiceEntityList.get(i).getMiroservice())
                    +"&namespace="+(StringUtils.isBlank(miroServiceEntityList.get(i).getNamespace())?"":miroServiceEntityList.get(i).getNamespace())
                    +"&containerName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getContainerName())?"":miroServiceEntityList.get(i).getContainerName())
                    +"&service="+(StringUtils.isBlank(miroServiceEntityList.get(i).getService())?"":miroServiceEntityList.get(i).getService())
                    +"&hostid="+(StringUtils.isBlank(miroServiceEntityList.get(i).getHostid())?"":miroServiceEntityList.get(i).getHostid())
                    +"&port="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPort())?"":miroServiceEntityList.get(i).getPort())
                    +"&podName="+(StringUtils.isBlank(miroServiceEntityList.get(i).getPodName())?"":miroServiceEntityList.get(i).getPodName());
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
            String result = responseEntity.getBody();
           if("1".equals(result)&&StringUtils.isNotBlank(result)){
               dao++;
           }
        }
        return dao;
    }


}
